﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Butcher.winform.EnterpriseForms.ScaleBackfill;
using System.Text.Json;
using Traceability.Commont.Domains.AnimalsDomainFiles;

namespace Butcher.winform.EnterpriseForms
{
    public partial class ApproachBackfill : Form
    {
        public static string value="";
        public ApproachBackfill()
        {
            InitializeComponent();
        }
        public class approached
        {
            public int pageIndex { get; set; } = 1;

            public int pageSize { get; set; } = 50;
            public DateTime? ApproachTime { get; set; } = null;
            public string? Suppliers { get; set; } = null;
            public string? SettlementType { get; set; } = null;
            public int? ApproachStatus { get; set; } = 0;
        };
        private void uiFlowLayoutPanel1_Load(object sender, EventArgs e)
        {
            int counts = 0;
            Approach approach = new Approach();
            approached approa = new approached();
            string url = @"http://localhost:5229/api/Enterprise/SelectApproachList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<approached>(approa), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["lists"].Value<JArray>();
            var count = josnresult["totalCount"];
            counts = Convert.ToInt32(count);
            List<Approach> ssss = results.ToObject<List<Approach>>();

            UIFlowLayoutPanel[] appro = new UIFlowLayoutPanel[counts];
            for (int t = 0; t < appro.Length; t++)
            {
                //Approach asas = ssss[t];
                //appro[t].Name = Json.Serialize(asas);
                //Approach sert = Json.Deserialize<Approach>(btn[t].Name);
                UIFlowLayoutPanel uiflow = new UIFlowLayoutPanel();
                uiflow.Size = new Size(200, 200);
                Button[] btn = new Button[appro.Length];
                btn[t] = new Button();
                btn[t].Size = new Size(100, 30);

                Approach asas = ssss[t];
                //转换为字符串
                btn[t].Name = Json.Serialize(asas);
                //转换为实体
                Approach babab = Json.Deserialize<Approach>(btn[t].Name);
                btn[t].Text = "提交";
                for (int i = 0; i <= 5; i++)
                {
                    Label newlabel = new Label();
                    Label newlabel2 = new Label();
                    newlabel.Width = 70;
                    newlabel2.Width = 80;
                    
                    if (i == 0)
                    {
                        newlabel.Text = "进场编号:";
                        newlabel2.Text = ssss[t].ApproachNo;
                    }
                    else if (i == 1)
                    {
                        newlabel.Text = "供货商:";
                        newlabel2.Text = ssss[t].Suppliers;
                    }
                    else if(i == 2)
                    {
                        newlabel.Text = "进场批次:";
                        newlabel2.Text = ssss[t].ApproachBatch;
                    }
                    else if(i == 3)
                    {
                        newlabel.Text = "进场头数:";
                        newlabel2.Text = ssss[t].Approachnum.ToString();
                    }
                    else if(i == 4)
                    {
                        newlabel.Text = "车牌号:";
                        newlabel2.Text = ssss[t].licensePlate;
                    }
                    uiflow.Controls.Add(newlabel);
                    uiflow.Controls.Add(newlabel2);
                }
                btn[t].Click += new System.EventHandler(this.Button_Click);
                uiflow.Controls.Add(btn[t]);
                uiFlowLayoutPanel1.Controls.Add(uiflow);
            }
        }
        private void Button_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            MessageBox.Show(button.Name);
            value = button.Name;
            LiveAnimalweighings departmentview = new LiveAnimalweighings();
            this.Hide();
            departmentview.ShowDialog();
        }
    }
}
